package com.weiadmin.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiadmin.response.RestResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 基础控制器
 * @author: shizhongyu
 * @date: 2023年05月27日 14:05
 */
public class BaseController {
//    abstract dao;
    /**
     * 页面跳转
     */
    public ModelAndView display(String path) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        return modelAndView;
    }

    /**
     * 带参数的页面跳转
     */
    public ModelAndView display(String path, Map<String, ?> params) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        modelAndView.addAllObjects(params);
        return modelAndView;
    }

    /**
     * 重定向
     * @param path
     * @return
     */
    public ModelAndView redirect(String path){
        return   new ModelAndView("redirect:" +path);
    }

    /**
     * 重定向
     * @param path
     * @param params
     * @return
     */
    public ModelAndView redirect(String path,Map<String, ?> params){
        ModelAndView modelAndView = new ModelAndView("redirect:" +path);
        modelAndView.addAllObjects(params);
        return  modelAndView;
    }

    public ResponseEntity<?> success(String msg,Object data)
    {
        RestResponse<Object> response = new RestResponse<>();
        return response.success(msg,data);
    }

    public ResponseEntity<?> success(String msg)
    {
        RestResponse<Object> response = new RestResponse<>();
        return response.success(msg);
    }

    public ResponseEntity<?> error(String msg,Object data)
    {
        RestResponse<Object> response = new RestResponse<>();
        return response.error(msg,data);
    }

    public ResponseEntity<?> error(String msg)
    {
        RestResponse<Object> response = new RestResponse<>();
        return response.error(msg);
    }

    public ResponseEntity<?> page(String msg, Page<?> pageData)
    {
        RestResponse<Object> response = new RestResponse<>();
        return response.page(msg,pageData.getRecords(),pageData.getTotal());
    }

//    @DeleteMapping("/delete/{id}")
//    public ResponseEntity<?> delete(@PathVariable String id)
//    {
//        if(StringUtils.isEmpty(id)){
//            return error("ID不能为空！");
//        }
//        int result = dao.deleteById(id);
//        if(result>0){
//            return success("删除成功！");
//        }else{
//            return error("删除失败,请稍后再试！");
//        }
//    }

}
